package com.yzy.mall.repository

import com.google.gson.Gson
import com.yzy.baselibrary.http.RetrofitAPi
import com.yzy.mall.http.response.BaseResponse
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody


class CardRepository : BaseRepository() {
    private object SingletonHolder {
        val holder = CardRepository()
    }

    companion object {
        val instance = SingletonHolder.holder
    }

    private val service: CardService = RetrofitAPi.instance.getApi(CardService::class.java)


    suspend fun getCard(
        idCard: String
    ): BaseResponse<CardBean> {
        return service.card(idCard)
    }

    suspend fun iscard(
        idCard: String
    ): InfoBean {
        return service.iscard(idCard)
    }


    suspend fun wineMembers(
        idCard: String, phoneNo: String, userName: String, receivingAddress: String,
        residualPoints: Int? = 0,
        accumulatedPoints: Int? = 0
    ): BaseResponse<Any> {
        val toJson = Gson().toJson(
            Card(
                idCard = idCard,
                phoneNo = phoneNo,
                userName = userName,
                varreceivingAddress = receivingAddress,
                residualPoints = 0,
                accumulatedPoints = 0
            )
        )
        val body: RequestBody =
            RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(), toJson)
        return service.wineMembers(
            body
        )
    }

    suspend fun exchangeInfo(
       saveBean: SaveBean
    ): BaseResponse<Any> {
        val toJson = Gson().toJson(saveBean)
        val body: RequestBody =
            RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(), toJson)
        return service.exchangeInfo(
            body
        )
    }
}



